const sass = require('sass');
const loadGruntTasks = require('load-grunt-tasks')
const cwd = process.cwd();
//读取配置文件
let config = require("../grunt.config.js");
try {
    const loadConfig = require(`${cwd}/grunt.config.js`);
    config = Object.assign({}, config, loadConfig)
} catch (e) { }

module.exports = grunt => {
    //任务配置，所有插件的配置信息
    grunt.initConfig({
        //获取package.json的信息
        pkg: grunt.file.readJSON('package.json'),
        clean: ['dist', '.tmp', 'release'],
        sass: {
            options: {
                sourceMap: true,
                implementation: sass
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: "src/",
                    src: ['assets/styles/*.scss'], //所有scss文件
                    dest: 'dist/',  //输出到此目录下
                    ext: '.css'
                }]
            }
        },
        cssmin: {
            options: {
                mergeIntoShorthands: false,
                roundingPrecision: -1
            },
            target: {
                files: [{
                    expand: true,
                    cwd: 'dist/',
                    src: ['assets/styles/*.css'],
                    dest: 'dist/',
                    ext: '.min.css'
                }]
            }
        },
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: "src/",
                    src: ['assets/scripts/*.js'], //所有js文件
                    dest: 'dist/'  //输出到此目录下
                }]
            }
        },
        uglify: {
            options: {
                mangle: true, //混淆变量名
                comments: 'false' //false（删除全部注释），some（保留@preserve @license @cc_on等注释）
            },
            target: {
                files: [{
                    expand: true,
                    cwd: "dist/",
                    src: ['assets/scripts/*.js'], //所有js文件
                    dest: 'dist/',  //输出到此目录下
                    ext: '.min.js'
                }]
            }
        },
        swigtemplates: {
            options: {
                defaultContext: config.data
            },
            production: {
                dest: 'dist/',
                src: ['src/*.html']
            }

        },
        htmlmin: {                                     // Task
            dist: {                                      // Target
                options: {                                 // Target options
                    removeComments: true,
                    collapseWhitespace: true
                },
                files: [{
                    expand: true,
                    cwd: "dist/src",
                    src: ['*.html'], //所有js文件
                    dest: 'dist/',  //输出到此目录下
                }]
            },
        },
        watch: {
            livereload: {
                options: {
                    livereload: '<%=connect.server.options.livereload%>'  //监听前面声明的端口  35729
                },
                files: [  //下面文件的改变就会实时刷新网页
                    'dist/*'
                ]
            },
            options: {
                livereload: true,
            },
            html: {
                files: ['src/*.html'],
                tasks: ['swigtemplates', 'htmlmin']
            },
            js: {
                files: ['src/assets/scripts/*.js'],
                tasks: ['babel', 'uglify']
            },
            css: {
                files: ['src/assets/styles*.scss'],
                tasks: ['sass', 'cssmin']
            }
        },
        extra: {
            main: {
                files: [
                    // includes files within path
                    { expand: true, cwd: "public", src: ['*'], dest: 'dist/', filter: 'isFile' },
                ],
            },
        },
        imagemin: {
            /* 压缩图片大小 */
            dist: {
                options: {
                    optimizationLevel: 1, //定义 PNG 图片优化水平
                    // use: [mozjpeg()]
                },
                files: [{
                    expand: true,
                    cwd: 'src/',//原图存放的文件夹
                    src: ['assets/images/*', "assets/fonts/*"], // 优化 img 目录下所有 png/jpg/jpeg/gif图片
                    dest: 'dist/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },

        connect: {
            server: {
                options: {
                    port: 8000,
                    hostname: '*',
                    livereload: 35729,
                    open: true, //自动打开网页 http://,
                    base: "dist"
                }
            }
        }
    })

    loadGruntTasks(grunt) // 自动加载所有的 grunt 插件中的任务

    grunt.registerTask("styles", ['sass', 'cssmin']);
    grunt.registerTask('scripts', ['babel', 'uglify'])
    grunt.registerTask('pages', ['swigtemplates', 'htmlmin'])
    grunt.registerTask('compile', ['clean', 'styles', 'scripts', 'pages'])
    grunt.registerTask('serve', [
        'connect',
        'watch'
    ]);


    grunt.registerTask('develop', ['clean', 'compile', 'serve'])

    grunt.registerTask('build', ['compile', 'extra', 'imagemin'])
}